package cn.xshi.monitor.web;

import cn.xshi.monitor.service.GraphSiteService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.monitor.model.GraphSite;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 站点
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/graphSite")
@Api(value = "站点",tags = "站点", description = "站点")
public class GraphSiteController extends BaseAction {

	@Resource
	private GraphSiteService graphSiteService;

	/**
	 * 查询并分页
	 * @param baseSearch
	 */
	@ApiOperation(value="查询并分页", notes="查询并分页")
	@PostMapping(value="/list")
	@Auth(value = "/graphSite/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<GraphSite>> getGraphSiteListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<GraphSite> graphSiteList = graphSiteService.getGraphSiteListByCondition(condition);
		for(GraphSite graphSite:graphSiteList){
			if(!StringUtil.isEmpty(graphSite.getCreateId())){
				OauthAccountEntity createBy = getAccount(graphSite.getCreateId());
				if(null != createBy){
					graphSite.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(graphSite.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(graphSite.getUpdateId());
				if(null != modifiedBy){
					graphSite.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<GraphSite> page = new PageInfo<GraphSite>(graphSiteList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	 * 查询单条记录
	 * @param id
	 */
	@ApiOperation(value="查询单条记录", notes="查询单条记录")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/graphSite/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<GraphSite> getGraphSiteById(@PathVariable("id")String id){

		GraphSite graphSite = graphSiteService.getGraphSiteById(id);

		if(!StringUtil.isEmpty(graphSite.getCreateId())){
			OauthAccountEntity createBy = getAccount(graphSite.getCreateId());
			if(null != createBy){
				graphSite.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(graphSite.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(graphSite.getUpdateId());
			if(null != modifiedBy){
				graphSite.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(graphSite);
	}

	/**
	 * 添加
	 * @param graphSite
	 */
	@ApiOperation(value="添加", notes="添加")
	@PostMapping(value="/add")
	public BaseResult addGraphSite(@RequestBody GraphSite graphSite){
		int i = 0;
		if(null != graphSite){
			i=graphSiteService.addGraphSite(graphSite);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 修改
	 * @param graphSite
	 */
	@ApiOperation(value="修改", notes="修改")
	@PutMapping(value="/update")
	public BaseResult updateGraphSite(@RequestBody GraphSite graphSite){
		int i = 0;
		if(null != graphSite){
			i=graphSiteService.updateGraphSite(graphSite);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@ApiOperation(value="删除", notes="删除")
	@DeleteMapping(value="/delete")
	public BaseResult delGraphSite(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=graphSiteService.delGraphSite(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 查询全部
	 */
	@ApiOperation(value="查询全部", notes="查询全部")
	@PostMapping(value="/substations")
	@Auth(value = "/graphSite/substations",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<GraphSite>> substations(){
		Map<String,Object> condition = new HashMap<>();
		List<GraphSite> graphSiteList = graphSiteService.getGraphSiteListByCondition(condition);
		return new BaseResult(graphSiteList);
	}
}
